﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class HotFixUi : Window
{
   private HotFixPanel m_Panel;
   private float m_SumTime;
   public override void Awake(params object[] paralist)
   {
      m_Panel = GameObject.GetComponent<HotFixPanel>();
      m_Panel.m_Image.fillAmount = 0;
      m_Panel.m_Tex.text = string.Format("{0:F}M/S", 0);
      HotPathcManager.Instance.ServerInfoError += ServerInfoError;
      HotPathcManager.Instance.ItemError += ItemError;
      // HotFix();

      if (HotPathcManager.Instance.ComputeUnPackFile())
      {
         m_Panel.m_SliderTopTex.text = "解压中。。。";
         HotPathcManager.Instance.StartUnPackFile((() =>
         {
            HotFix();
         }));
      }
      else
      {
         HotFix();
      }
   }

   void HotFix()
   {
      if (Application.internetReachability == NetworkReachability.NotReachable)
      {
         //提示网络错误，检测网络链接是否正常
         GameStart.OpenCommonConfirm("网络链接失败", "网络链接失败，请检查网络链接是否正常？", () =>
            { Application.Quit(); }, () => { Application.Quit(); });
      }
      else
      {
         CheckVersion();
      }
   }

   void CheckVersion()
   {
         HotPathcManager.Instance.CheckVersion((hot) =>
         {
            if (hot)
            {
               //提示玩家是否确定热更下载
               GameStart.OpenCommonConfirm("热更确定", string.Format("当前版本为{0},有{1:F}M大小热更包，是否确定下载？", HotPathcManager.Instance.CurVersion, HotPathcManager.Instance.LoadSumSize / 1024.0f), OnClickStartDownLoad, OnClickCancleDownLoad);

            }
            else
            {
               // 进入游戏
               StartOnFinish();
            }
         });
   }

   /// <summary>
   /// 取消下载
   /// </summary>
   private void OnClickCancleDownLoad()
   {
      Application.Quit();
      
   }
   
   /// <summary>
   /// 确认下载
   /// </summary>
   private void OnClickStartDownLoad()
   {
      if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
      {
         if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
         {
            GameStart.OpenCommonConfirm("下载确认", "当前使用的是手机流量，是否继续下载？", StartDownLoad, OnClickCancleDownLoad);
         }
      }
      else
      {
         StartDownLoad();
      }
   }

   void StartDownLoad()
   {
      m_Panel.m_SliderTopTex.text = "下载中。。。";
      m_Panel.m_InfoPanel.SetActive(true);
      m_Panel.m_HotContentTex.text = HotPathcManager.Instance.CurrentPatches.Des;
      GameStart.Instance.StartCoroutine(HotPathcManager.Instance.StartDownLoadAB(StartOnFinish));
   }

   void StartOnFinish()
   {
      GameStart.Instance.StartCoroutine(OnFinish());
   }

   IEnumerator OnFinish()
   {
      yield return GameStart.Instance.StartCoroutine(GameStart.Instance.StartGame(m_Panel.m_Image, m_Panel.m_SliderTopTex));
      UIManager.Instance.CloseWnd(this);
   }
   public override void OnClose()
   {
      HotPathcManager.Instance.ServerInfoError -= ServerInfoError;
      HotPathcManager.Instance.ItemError -= ItemError;
      //加载场景
      GameMapManager.Instance.LoadScene(ConStr.MENUSCENE);
   }

   void ServerInfoError()
   {
      GameStart.OpenCommonConfirm("服务器列表获取失败", "服务器列表获取失败，请检查网络链接是否正常？尝试重新下载！", CheckVersion, Application.Quit);
   }

   void ItemError(string all)
   {
      GameStart.OpenCommonConfirm("资源下载失败", string.Format("{0}等资源下载失败，请重新尝试下载！", all), AnewDownload, Application.Quit);
   }

   private void AnewDownload()
   {
      HotPathcManager.Instance.CheckVersion((hot) =>
      {
         if (hot)
         {
            StartDownLoad();
         }
         else
         {
            StartOnFinish();
         }
      });
   }

   public override void OnUpdate()
   {
      if (HotPathcManager.Instance.StartUnPack)
      {
         m_SumTime += Time.deltaTime;
         m_Panel.m_Image.fillAmount = HotPathcManager.Instance.GetUnpackProgress();
         float speed = (HotPathcManager.Instance.AlreadyUnPackSize / 1024.0f)/ m_SumTime;
         m_Panel.m_Tex.text = string.Format("{0:F} M/S", speed);
      }

      if (HotPathcManager.Instance.StartDownload)
      {
         m_SumTime += Time.deltaTime;
         m_Panel.m_Image.fillAmount = HotPathcManager.Instance.GetProgress();
         float speed = (HotPathcManager.Instance.GetLoadSize() / 1024.0f) / m_SumTime;
         m_Panel.m_Tex.text = string.Format("{0:F} M/S", speed);
      }
   }
}
